package mediadisplay.menusystem 
{
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class Menu extends Sprite
	{
		
		private var menufields:Array = new Array;
		private var _headline:String;
		
		private var tf:TextField;
		private var textformat:TextFormat;
		public var main:Main;
		
		public function Menu(main:Main, headline:String) 
		{
			this.main = main;
			_headline = headline;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			textformat = new TextFormat();
			textformat.size = 14;
			textformat.color = 0xff0000;
			
			tf = new TextField();
			addChild(tf);
			tf.defaultTextFormat = textformat;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.mouseEnabled = false;
			tf.selectable = false;	
			tf.text = _headline;
			
			addEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
		}
		
		private function mouseoverHandler(e:MouseEvent):void 
		{
			for (var i:int = 0; i < menufields.length; i++) 
			{
				var menufield:MenuField = menufields[i];
				menufield.visible = true;
			}			
		}
		
		private function mouseoutHandler(e:MouseEvent):void 
		{
			for (var i:int = 0; i < menufields.length; i++) 
			{
				var menufield:MenuField = menufields[i];
				menufield.visible = false;
			}
		}
		public function drawMenu():void {
			var fType:String = GradientType.LINEAR;
			var colors:Array = [ 0xffffff, 0 ];
			var alphas:Array = [ 1, 1 ];
			var ratios:Array = [ 0, 255 ];
			var matr:Matrix = new Matrix();
			matr.createGradientBox( 100, 20, 0, 0, 0 );
			matr.rotate(Math.PI/2);
			var sprMethod:String = SpreadMethod.PAD;
			
			graphics.lineStyle(1, MenuSettings.MENU_FRAME_COLOR);
			//graphics.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
			graphics.beginFill(0xeeeeee);
			graphics.drawRect(0, 0, MenuSettings.MENU_WIDTH, 20);
			graphics.endFill();	
			
			// position menufields
			for (var i:int = 0; i < menufields.length; i++) 
			{
				var menufield:MenuField = menufields[i];
				menufield.y = 20 + (i * 20);
			}
		}
		public function add(tuneID:int, txt:String, url:String):void {
			var menufield:MenuField = new MenuField(this, tuneID, txt, url);
			addChild(menufield);
			menufield.visible = false;
			menufields.push(menufield);
		}
		
	}

}